/*
 *
 * scumix.swat.spark.PopupMenu
 * 弹出菜单
 *
 * author: scumyang
 * create: 2011-04-24
 * update:
 * notes : 需导入 scumix.swat.swat3.js
 *
 */

/*
用法:
	
1.声明变量:
var t = null;
var menuData =
{
	name:'mnMain',
	width:150,
	items:
	[
		{caption:'查看', icon:''},
		{caption:'批量设置类别', icon:'res/icon/icon-1038.gif'},
		{caption:'删除', icon:'', css:{color:'#c00'}},
		{caption:'-'},

		{caption:'属性', icon:'', 
			onClick:function(D)
			{
				alert( D.caption );
			}
		}
	]
}

2.初始化:
$.pageReady(function()
{
	document.oncontextmenu = function(E){ return false; }
	t = new scumix.swat.spark.PopupMenu( menuData );
});

3.显示:
show( X, Y )

*/

scumix.swat.spark.PopupMenu = $.newClass();
scumix.swat.spark.PopupMenu.prototype =
{
	constructor: function( D )
	{
		this.id = $.makeGlobalId( 'SxPopupMenu' );
		this.ElName = this.id;

		this.width = D ? D.width || 100 : 100;

		this.initialized = false;
		this.isVisible = false;

		this.EFrame = null;

		if( D && D.items && D.items.length ) this.initView( D );
	},

	initView: function( D )
	{
		// 主容器
		var oFrame = $.createElement({type:'div', id:this.ElName, css:'zIndex:9', attr:{className:'SxPopupMenu'}});
		this.EFrame = oFrame;
		var ro = $(oFrame);
		ro.css( 'opacity', '90' );
		ro.css( 'width', this.width );

		document.body.appendChild( oFrame );
		$(document).addEvent( 'click', this.DoClickOut.bind(this));

		// 添加初始菜单项
		for( var i=0; i<D.items.length; i++ )
		{
			this.addItem( D.items[i] );
		}
	},

	// 添加菜单项
	addItem: function( D )
	{
		// 分隔符
		if( D.caption === '-' )
		{
			var oItem = $.createElement({type:'div', id:this.ElName, css:'', attr:{className:'SxPM_Sep'}});
		}

		// 普通项
		else
		{
			var oItem = $.createElement({type:'div', id:this.ElName, css:'', attr:{className:'SxPM_Item SxPM_Item_Normal'}});
			
			if( D.icon )
				oItem.innerHTML = '<div class="SxPM_ItemIcon" style="background:url(' + D.icon + ') no-repeat center"></div>' + D.caption;
			else
				oItem.innerHTML = '<div class="SxPM_ItemIcon"></div>' + D.caption;
			
			oItem.jsObject = this;
			oItem.jsData = D;

			// 扩展 css
			if( D.css )
			{
				var o = $(oItem);
				for( var i in D.css )
				{
					o.css( i, D.css[i] );
				}
			}

			$(oItem).addEvent( 'mouseover', this.doIOver );
			$(oItem).addEvent( 'mouseout', this.doIOut );
			$(oItem).addEvent( 'click', this.doIClick );
		}

		this.EFrame.appendChild( oItem );
		this.menuHeight = $(this.EFrame).css('height');
	},

	// 鼠标移入
	doIOver: function( E )
	{
		var o = this;
		o.className = 'SxPM_Item SxPM_Item_Over';
	},

	// 鼠标移出
	doIOut: function( E )
	{
		var o = this;
		o.className = 'SxPM_Item SxPM_Item_Normal';
	},

	// 鼠标点击
	doIClick: function( E )
	{
		var o = this;
		o.jsObject.hide( o.jsData.onClick, o.jsData );
		//if( o.jsData.onClick ) o.jsData.onClick( o.jsData );
	},

	// 隐藏
	hide: function( F, A )
	{
		//$(this.ElName).hide();
		var I = this;
		$(I.ElName).anim({opacity:1,height:0},10,function(){$(I.ElName).hide(); if(F)F(A);});
		this.isVisible = false;
	},

	// 显示
	show: function( X, Y )
	{
		$(this.EFrame).moveTo( X, Y ).show();
		var I = this;
		$(I.ElName).anim({opacity:90,height:I.menuHeight},10);
		this.isVisible = true;
	},

	// 界外点击
	DoClickOut: function( E )
	{
		if( !this.isVisible ) return;
		var e = $.getTarget(E);
		if(  !$(this.EFrame).ptIn( E ) && $.getTarget(E) != this.EBind ) this.hide();
	}
}
